// Surroundings/productDetail/productDetail.js
var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var user = require('../../services/user.js');
var WxParse = require('../../lib/wxParse/wxParse.js');
var app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        info:{},
        disabled:false,
        memberId:null,
        sourceId:null,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log("enter onLoad")
        this.getData(options.id)
        const userInfo = wx.getStorageSync("userInfo");
        this.setData({
            id:options.id,
            userInfo:userInfo,
            memberId:userInfo.id,
        })
        if(options.sourceId){
            this.setData({
                sourceId:options.sourceId
            })
        }
    },
    getData: function(id){
        let _this = this;
        wx.showLoading({
          title: '加载中...',
        })
        util.request(api.surroundings.getMerchantInfoDetail+'/'+id,{},'post').then(res=>{
            //res.result.graphicDetails = res.result.graphicDetails.replace(/\<img/gi, '<img class="my-rich-img" ')
            _this.setData({
                info:res.result
            })
            if (res.result.graphicDetails) {
                WxParse.wxParse("goodsDetail", "html", res.result.graphicDetails, _this);
            }
            wx.hideLoading()
        }).catch(err=>{
            console.log(err)
        })
    },
    onBuy(){
        let _this = this;
        wx.showLoading({
            title: '购买中...',
          })
        this.setData({
            disabled:true
        })
        var sourceId = this.data.sourceId;
        util.request(api.surroundings.userBuy,{
            sourceId:sourceId,
            item:'',
            merchantCommodityId:this.data.info.id
        },'post').then(res=>{
            wx.requestPayment({
                'timeStamp': res.result.timeStamp,
                'nonceStr': res.result.nonceStr,
                'package': res.result.packageValue,
                'signType': res.result.signType,
                'paySign': res.result.paySign,
                'success': function (res) {
                    _this.setData({
                        disabled:false
                    })
                    wx.hideLoading()
                    //订阅消息
                    let templIds = ['qUgT6qfdGEhZwW7iHPcwzFAUq9EElyS3oP_8EzMlINE']
                    wx.requestSubscribeMessage({
                        tmplIds: templIds,
                        success (res) {
                            console.log(res)
                            wx.navigateTo({
                                url: '/Surroundings/orderResult/orderResult',
                            })
                        },
                        fail (err) {
                            console.log(err)
                            wx.navigateTo({
                                url: '/Surroundings/orderResult/orderResult',
                            })
                        }
                    })
                },
                'fail': function (res) {
                    wx.hideLoading()
                    wx.showToast({
                        title: "支付失败",
                        icon: "none"
                    })
                },
                'complete': function (res) {
                  reject(res);
                }
            });
        }).catch(err=>{
            console.log(err)
            wx.showToast({
              title: '购买失败，请重新购买～',
            })
            wx.hideLoading()
            this.setData({
                disabled:false
            })
        })
    },
    onFillInfo(){
        const sourceId = this.data.sourceId;
        console.log("sourceId is ",sourceId);
        wx.navigateTo({
          url: '/Surroundings/memberInfo/memberInfo?merchantCommodityId='+this.data.info.id+'&sourceId='+sourceId+'&adultPrice='+this.data.info.adultPrice+'&childPrice='+this.data.info.childPrice
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        let id = this.data.id
        let memberId = this.data.memberId
        return {
            path: '/Surroundings/productDetail/productDetail?id='+id+'&sourceId='+memberId,
            title: '这个商品真的太划算了，我已经下单了！', //转发页面的标题
        }
    }
})
